<?php

function loadClass($classname) {
	var_dump($classname);

}
spl_autoload_register("loadClass");

/**
 * 发送post请求
 * @param string $url 请求地址
 * @param array $post_data post键值对数据
 * @return string
 */
function send_post($url, $post_data) {

	$postdata = http_build_query($post_data);
	$options = array(
		'http' => array(
			'method' => 'POST',
			'header' => 'Content-type:application/x-www-form-urlencoded',
			'content' => $postdata,
			'timeout' => 15 * 60, // 超时时间（单位:s）
		),
	);

	$context = stream_context_create($options);
	$result = file_get_contents($url, false, $context);

	return $result;
}

function send_get($url, $post_data) {

	$params = "";
	if (is_array($post_data)) {
		foreach ($post_data as $key => $value) {
		    $item=urlencode($value);
			$params .= "&{$key}={$item}";
		}
		$params = substr($params, 1, strlen($params) - 1);
	}

	$result = file_get_contents($url ."?". $params);



	return $result;

}


function send_test($url, $post_data) {

    $params = "";
    if (is_array($post_data)) {
        foreach ($post_data as $key => $value) {
            $item=urlencode($value);
            $params .= "&{$key}={$item}";
        }
        $params = substr($params, 1, strlen($params) - 1);
    }

    $result = file_get_contents($url ."?". $params);

    $data["result"]=$result;
    $data["url"]=$url ."?". $params;

    return $data;

}

/**
 * 手机号中间用星号替换
 * @param  [type] $phone [description]
 * @return [type]        [description]
 */
function replaceMobile($phone) {
	return substr_replace($phone, '****', 5, 4);

}

function Member() {
	return D('User/Member');
}

function cutstr_html($string) {

	$string = str_replace("\r", "", $string);

	return trim($string);
}

/**
 * 检测用户是否登录
 *
 */
function is_login() {
	$user = session('user_auth');

	if (empty($user)) {
		return 0;
	} else {
		return 1;

	}
}

/**
 * 检测用户是否登录
 *
 */
function is_Admin_login() {
	$user = session('user_auth_admin');

	if (empty($user)) {
		return 0;
	} else {
		return 1;

	}
}

/**
 * 检测是否在黑名单里
 *
 * @param  [type] $phone [description]
 * @return [type]        [description]
 */
function isBlackList($phone) {
	$model = M('cp.blacklist', 'cp_');
	return $model->where(array('phone' => $phone))->find();

}

/**
 * 文件上传错误信息
 * @param  [type] $error [description]
 * @return [type]        [description]
 */
function showUploadError($error) {
	$msg = "";
	switch ($error) {
	case '1':
		$msg = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
		break;
	case '2':
		$msg = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
		break;
	case '3':
		$msg = '文件只有部分被上传';
		break;
	case '4':
		$msg = '没有文件被上传';
		break;
	default:

		break;
	}
	return $msg;
}

function readExcel($filePath) {
	import('ORG.PHPExcel');
	$PHPExcel = new PHPExcel();

	/**默认用excel2007读取excel，若格式不对，则用之前的版本进行读取*/
	$PHPReader = new PHPExcel_Reader_Excel2007();
	if (!$PHPReader->canRead($filePath)) {
		$PHPReader = new PHPExcel_Reader_Excel5();
		if (!$PHPReader->canRead($filePath)) {
			echo 'no Excel';
			return;
		}
	}
	$c = array('A' => 'dcmobile', 'B' => 'dcprivence', 'C' => 'dccity', 'D' => 'dcarea');
	$PHPExcel = $PHPReader->load($filePath);
	/**读取excel文件中的第一个工作表*/
	$currentSheet = $PHPExcel->getSheet(0);
	/**取得最大的列号*/
	$allColumn = $currentSheet->getHighestColumn();
	/**取得一共有多少行*/
	$allRow = $currentSheet->getHighestRow();
	/**从第二行开始输出，因为excel表中第一行为列名*/
	$model = M('cp.privence', 'cp_');
	for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
		/**从第A列开始输出*/
		$data = array();
		for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) {
			$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue(); /**ord()将字符转为十进制数*/
			//	echo $currentColumn;
			//	echo $val."\t";
			switch ($currentColumn) {
			case 'A':
				$data['dcmobile'] = $val;
				break;
			case 'B':
				$data['dcprivence'] = $val;
				break;
			case 'C':
				$data['dccity'] = $val;
				break;
			case 'D':
				$data['dcarea'] = $val;
				break;

			default:
				# code...
				break;
			}
			//var_dump($data);

		}
		if (!$model->where($data)->find()) {
			if (!$model->add($data)) {
				echo $model->getDbError();
				return false;
			} else {
				var_dump($model->getDbError());
			}
		}else{
		    $model->where(array('dcmobile'=>$data['dcmobile']))->save($data);
        }

	}
	//echo "\n";
	return true;

}

/**对excel里的日期进行格式转化*/
function GetData($val) {
	$jd = GregorianToJD(1, 1, 1970);
	$gregorian = JDToGregorian($jd + intval($val) - 25569);
	return $gregorian; /**显示格式为 “月/日/年” */
}

function object_array($array) {
	if (is_object($array)) {
		$array = (array) $array;
	}
	if (is_array($array)) {
		foreach ($array as $key => $value) {
			$array[$key] = object_array($value);
		}
	}
	return $array;
}

/**
 * 导出数据为excel表格
 *@param $data    一个二维数组,结构如同从数据库查出来的数组
 *@param $title   excel的第一行标题,一个数组,如果为空则没有标题
 *@param $filename 下载的文件名
 *@examlpe
 *$stu = M ('User');
 *$arr = $stu -> select();
 *exportexcel($arr,array('id','账户','密码','昵称'),'文件名!');
 */
function exportexcel($data = array(), $title = array(), $filename = 'report') {
	header("Content-type:application/octet-stream");
	header("Accept-Ranges:bytes");
	header("Content-type:application/vnd.ms-excel");
	header("Content-Disposition:attachment;filename=" . $filename . ".xls");
	header("Pragma: no-cache");
	header("Expires: 0");
	//导出xls 开始
	if (!empty($title)) {
		foreach ($title as $k => $v) {
			$title[$k] = iconv("UTF-8", "GB2312", $v);
		}
		$title = implode("\t", $title);
		echo "$title\n";
	}
	if (!empty($data)) {
		foreach ($data as $key => $val) {
			foreach ($val as $ck => $cv) {
				$data[$key][$ck] = iconv("UTF-8", "GB2312", $cv);
			}
			$data[$key] = implode("\t", $data[$key]);

		}
		echo implode("\n", $data);
	}
}

function formatDate($date) {
	if (empty($date)) {
		return "";
	}
	return date('Y/m/d H:i:s', strtotime($date));
}
